﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RegisterationCommon.Common;
using RegisterationCommon.Student;
using System.Data;
using RegisterationCommon.Education;
using System.IO;

namespace RegisterationSystem.Space_Education
{
    public partial class special_student_register : System.Web.UI.Page
    {
        public string ShcJson = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Form["action"] != "")
                {
                    switch (Request.Form["action"])
                    {
                        case "pswdreset":
                            UpdStudentPswdReset();
                            break;
                        //case "derive_excelList":
                        //    derive_excelList();
                        //    break;
                        //case "derive_excelListAll":
                        //    derive_excelListAll();
                        //    break;
                    }
                }
                ShcJson = BaseLibrary.GetSclJson();
            }
        }


        //void DirectoryInfoList()
        //{
        //    int day = 0;
        //    DirectoryInfo theFolder = new DirectoryInfo(Server.MapPath(@"..\StudentInfoToExcel\"));
        //    FileInfo[] dirInfo = theFolder.GetFiles();
        //    //遍历文件夹
        //    foreach (FileInfo NextFile in dirInfo)
        //    {
        //        string filename = NextFile.Name;
        //        string[] Date = filename.Split('_');
        //        if (Date.Length > 1)
        //        {
        //            string strTimeNow = DateTime.Now.ToString("yyyyMMdd");
        //            int d1 = int.Parse(Date[1]);
        //            int d2 = int.Parse(strTimeNow);
        //            int ND = d2 - d1;
        //            day = ND;
        //            if (day >= 1)
        //            {
        //                File.Delete(Server.MapPath(@"..\StudentInfoToExcel\" + filename));
        //            }
        //        }
        //    }
        //}
        //void derive_excelList()
        //{
        //    DirectoryInfoList();
        //    //获取修改ID
        //    string stuID = "";
        //    string idlist = Request.Form["id"];
        //    string[] ids = idlist.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        //    for (int i = 0; i < ids.Length; i++)
        //    {
        //        if (i != (ids.Length - 1))
        //        {
        //            stuID = stuID + ids[i] + " or id=";
        //        }
        //        else
        //        {
        //            stuID = stuID + ids[i];
        //        }
        //    }
        //    DataTable dt = DBAction.GetDTFromSQL("Select * From " + ETableName.Students + " where id=" + stuID);
        //    EduExcelHelper excel = new EduExcelHelper();
        //    string Toexcelpath = excel.SCexcel(dt, Server.MapPath(@"..\StudentInfoToExcel\"));
        //    Response.Clear();
        //    string result = "..\\" + "StudentInfoToExcel\\" + Toexcelpath;
        //    Response.Write(result);
        //    Response.End();
        //}



        //void derive_excelListAll()
        //{
        //    DirectoryInfoList();
        //    //获取修改ID
        //    string where = Request.Form["where"];
        //    string typeid = Request.Form["typeid"];
        //    EduExcelHelper excel = new EduExcelHelper();
        //    DataTable dt = excel.GetUndistributedStuList(where, typeid);
        //    string Toexcelpath = excel.SCexcel(dt, Server.MapPath(@"..\StudentInfoToExcel\"));
        //    Response.Clear();
        //    string result = "..\\" + "StudentInfoToExcel\\" + Toexcelpath;
        //    Response.Write(result);
        //    Response.End();
        //}
        //根据id重置学生帐号密码
        void UpdStudentPswdReset()
        {
            string id = Request.Form["id"].ToString();
            string StudentPswd = Request.Form["StudentPswd"].ToString();
            StudentInfo s = new StudentInfo();
            s.MdfAccountPwd(Convert.ToInt64(id), BaseLibrary.ConvertToMD5(StudentPswd.ToLower()));
        }
        protected void btnadd_Click(object sender, EventArgs e)
        {
            DataTable dt = DBAction.GetDTFromSQL("select * from Students where IDCardNumber='" + txtIDCardNumber.Value.Trim() + "'");
            if (dt.Rows.Count > 0)
            {
                //已登记
                Message.Send(10076);
                EduAccount.AddEduSysLog(Cookies.Get("AccountName").ToString(), DateTime.Now, HttpContext.Current.Request.UserHostAddress, "学生信息代录", "该学生已登记");
                return;
            }
            StudentInfo.Current.TrueName = txtname.Value.Trim();//学生姓名
            StudentInfo.Current.IDCardNumber = txtIDCardNumber.Value.Trim();//学生身份证
            StudentInfo.Current.Sex = ddlsex.SelectedValue == "0" ? false : true;//性别0男1女
            //StudentInfo.Current.Hobby = txtHouseAreaInfo.Value;//兴趣爱好
            StudentInfo.Current.FirstGuardianName = txtFirstGuardianName.Value;//第一监护人姓名
            StudentInfo.Current.FirstGuardianUnit = txtFirstGuardianUnit.Value;//第一监护人工作单位
            StudentInfo.Current.FirstGuardianPhone = txtFirstGuardianPhone.Value;//第一监护人联系电话
            StudentInfo.Current.FirstRelationship = txtFirstRelationship.Value;  //第一监护人和他的关系
            StudentInfo.Current.SecGuardianName = txtSecGuardianName.Value;//第二监护人姓名
            StudentInfo.Current.SecGuardianUnit = txtSecGuardianUnit.Value;//第二监护人工作单位
            StudentInfo.Current.SecGuardianPhone = txtSecGuardianPhone.Value;//第二监护人联系电话
            StudentInfo.Current.SecRelationship = txtSecRelationship.Value;  //第二监护人和他的关系
            StudentInfo.Current.SchID = Convert.ToInt32(hidstuid.Value);//所属学校
           // StudentInfo.Current.Status = Convert.ToInt32(hidstuid.Value) == 0 ? 0 : 1;//信息状态(0未分配1已分配2初审通过3初审不通过)
            StudentInfo.Current.Status = 2;//信息状态(0未分配1已分配2初审通过3初审不通过)
            StudentInfo.Current.RegisterTime = DateTime.Now;//登记时间
            StudentInfo.Current.PhoneStatusText = "";//电话回访反馈说明
            StudentInfo.Current.PhoneStatus = false;//电话回访状态(0未回访1回访)
            StudentInfo.Current.IsDelete = false;//是否删除(0未删除 1删除)
            StudentInfo.Current.IsRecord = true;//是否代录
            StudentInfo.Current.IsMatured = true;
            StudentInfo.Current.StudentType = "";//是否是外籍学生
           if (Convert.ToInt32(hidhous.Value) == 3)
            {
                StudentInfo.Current.Birthday = DateTime.Parse(this.hiddate.Value);
                string NewPwd ="123456";//截取身份证生日作为密码
                StudentInfo.Current.LoginPswd = BaseLibrary.ConvertToMD5(NewPwd.ToLower());//保存密码
                StudentInfo.Current.StudentType = "wj";//是否是外籍学生
            }
            else
            {
                string NewPwd = StudentInfo.Current.IDCardNumber.Substring(6, 8);//截取身份证生日作为密码
                StudentInfo.Current.LoginPswd = BaseLibrary.ConvertToMD5(NewPwd.ToLower());//保存密码
                //获取身份证上的时间 进行比较 是否足龄
                string stuDate = StudentInfo.Current.IDCardNumber.Substring(6, 4) + "-" + StudentInfo.Current.IDCardNumber.Substring(10, 2) + "-" + StudentInfo.Current.IDCardNumber.Substring(12, 2);
                StudentInfo.Current.Birthday = Convert.ToDateTime(stuDate); //出生年月
                //足龄条件
                string yea = (DateTime.Now.Year - 6) + "-08-31";
                //身份证时间和足龄条件进行对比
                int isyea = DateTime.Compare(Convert.ToDateTime(yea), Convert.ToDateTime(stuDate));
                //StudentInfo.Current.IsMatured = isyea > 0 ? true : false;//是否足龄
            }
            StudentInfo.Current.HouseAreaID = Convert.ToInt32(hidhous.Value);   //户口所在地(0经济技术开发区1市内其它区2省内市外、外省外市)
            StudentInfo.Current.ScommAreaID = Convert.ToInt32(hidsid.Value);   //住房社区
            StudentInfo.Current.XcommAreaID = Convert.ToInt32(hidxid.Value);   //住房小区
            StudentInfo.Current.LevelNum = 0;   //住房社区
            StudentInfo.Current.ScommName = hidsname.Value;//社区名称
            StudentInfo.Current.XcommName = hidxname.Value;//小区名称
            StudentInfo.Current.SignUpNum = "42011111";//登记单号
            //StudentInfo.Current.HouseNumber = txtHouseNumber.Value;//门牌号
            StudentInfo.Current.HouseNumber = txtHouseNumber.Value ;//门牌号,如：M-M-M
            StudentInfo.Current.HouseCard = txtHouseCard.Value;//房产号
            StudentInfo.Current.Level = -1;//住房社区
            StudentInfo.Current.Adjustment = "";//是否服从调剂
            StudentInfo.Current.Level = 3;//学生等级3为d类
            StudentInfo.Current.HousingOccupancyTime = "";//住房入住时间
            StudentInfo.Current.ServicesDivision = 0;// 是否进行了服务划分（0，没有；1划分了）

            StudentInfo.Current.IsSelf = Convert.ToInt32(radio.Value);//是否为自有房产（0是1不是）
            StudentInfo.Current.Ext1 = txt.Value;//单独存储门栋，方便以后的服务划分，按照门栋
            StudentInfo.Current.Ext2 = "0";//教育局手动分配
            StudentInfo.Current.Ext3 = "0";//0为代录
            StudentInfo.Current.Ext4 = "0";//0为特殊人才
            StudentInfo.Current.Ext5 = "";//

            //switch (StudentInfo.Current.HouseAreaID)
            //{
            //    case 0:
            //        StudentInfo.Current.Level = 0;//学生等级
            //        break;
            //    case 1:
            //        StudentInfo.Current.Level = 1;//学生等级
            //        break;
            //    case 2:
            //        StudentInfo.Current.Level = 1;//学生等级
            //        break;
            //    default:
            //        StudentInfo.Current.Level = 0;//学生等级
            //        break;
            //}
            //if (StudentInfo.Current.ScommName == "其他" && StudentInfo.Current.HouseAreaID == 2)
            //{
            //    StudentInfo.Current.Level = 2;
            //    if (hidAdjustment.Value.ToString().Trim() == "0")
            //    {
            //        StudentInfo.Current.Adjustment = "服从";
            //    }
            //    else
            //    {
            //        StudentInfo.Current.Adjustment = hidAdjustment.Value.ToString().Trim();
            //    }
            //}
            if (StudentInfo.Current.AddNewForeignStudentInfo())
            {
                Message.Send(10066);
                EduAccount.AddEduSysLog(Cookies.Get("AccountName").ToString(), DateTime.Now, HttpContext.Current.Request.UserHostAddress, "学生信息代录", "该学生已代录成功");
            }
            else
            {
                //失败
                Message.Send(10067);
                EduAccount.AddEduSysLog(Cookies.Get("AccountName").ToString(), DateTime.Now, HttpContext.Current.Request.UserHostAddress, "学生信息代录", "该学生已代录失败");
            }
        }
    }
}